home *** CD-ROM | disk | FTP | other *** search
/ 9-Digit Zip Code Directory / 9-Digit Zip Code Directory (American Business Information) (ABIZIP-12).ISO / z4src.zip / DIDIRRD.C < prev    next >
C/C++ Source or Header  |  1993-08-13  |  5KB  |  152 lines

  1. //----------------------------------------------------------------------------
  2. //                            MODULE DESCRIPTION
  3. //
  4. //  Module:    didirrd.c
  5. //   Title:    Data File I/O Library
  6. //  Notice:    John M. Weeder
  7. //                 Copyright (c) 1993. All rights reserved.
  8. //             This module contains proprietary information and should be 
  9. //                treated as confidential.
  10. //
  11. //----------------------------------------------------------------------------
  12. //                           MAINTENANCE HISTORY
  13. //
  14. // $Workfile$
  15. // $Revision$
  16. //   $Author$
  17. //     $Date$
  18. //      $Log$
  19. //
  20. //----------------------------------------------------------------------------
  21. //                             MODULE NARRATIVE
  22. //
  23. //
  24. //    This module contains code to read a directory entry.
  25. //
  26. //    The code in this module should be written entirely in C. 
  27. //    Do not use any C++ constructs.
  28. //
  29. //    This module is portable to:
  30. //        DOS 3.X+
  31. //        MS Windows 3.X+
  32. //        OS/2 2.X+
  33. //        OS/2 2.0 PM
  34. //        SCO UNIX.
  35. //
  36. //    The following compilers are supported:
  37. //        MSC 6.0A
  38. //        MSC/C++ 7.0
  39. //        Borland C++ 3.1 for DOS
  40. //        Borland C++ 1.0 for OS/2 2.X
  41. //        SCO UNIX cc
  42. //
  43. //----------------------------------------------------------------------------
  44. #include <di.h>
  45.  
  46.  
  47. //----------------------------------------------------------------------------
  48. //   Description:    Read directory entry
  49. //    Parameters:    hpf        Physical file handle
  50. //                        cDir        Directory entry to read
  51. //                        pdir        Buffer to receive directory entry
  52. //       Returns:    TRUE if successful.
  53. //----------------------------------------------------------------------------
  54. BOOL FN_E DioDirRead(HPF hpf, SIZET cDir, PDATADIR pdir)
  55. {
  56.     DATAHDR hdr;
  57.     FPOS fpos;
  58.     SIZET i;
  59.                                                     // Validate file handle
  60.     Assert(hpf >= 0 && hpf < MAX_PHYSICAL_FILES);    
  61.     Assert(di.physical[hpf].fUsed);
  62.     if (di.physical[hpf].fl & PF_ERROR)
  63.         return FALSE;
  64.     if (!DioHeaderRead(hpf, &hdr))        // Read header information
  65.         return FALSE;
  66.     Assert(pdir);
  67.     Assert(cDir < hdr.usDirectoryEntries);
  68.  
  69.     //
  70.     //    If physical file directory is not cached, read it in.
  71.     //
  72.     if (di.physical[hpf].pdatadir == NULL)
  73.         {
  74.         SIZET cBuf = (SIZET)hdr.usDirectoryEntries * sizeof(DATADIR);
  75.         SIZET cEntry = sizeof(DATADIR) - sizeof(ULONG);
  76.                                                     // Allocate a buffer
  77.         di.physical[hpf].pdatadir = (PDATADIR)MemAlloc(cBuf);
  78.         if (di.physical[hpf].pdatadir == NULL)
  79.             {
  80.             ErrorNoMem();
  81.             goto ERROR_EXIT;
  82.             }
  83.         fpos = (LONG)sizeof(DATAHDR);
  84.         if (!FileRead(di.physical[hpf].hf, di.physical[hpf].pdatadir, cBuf, fpos))
  85.             goto ERROR_EXIT;
  86.                                                     // Validate CRC codes for entries
  87.         for (i = 0; i < (SIZET)hdr.usDirectoryEntries; ++i)
  88.           if (di.physical[hpf].pdatadir[i].crc
  89.             != (ULONG)CrcCalc((PBYTE)&di.physical[hpf].pdatadir[i], cEntry))
  90.               {
  91.               Error("Data file is corrupt.\nDirectory entry CRC code does not verify.");
  92.               goto ERROR_EXIT;
  93.               }
  94.         }                                            // Return directory entry contents
  95.     *pdir = di.physical[hpf].pdatadir[cDir];
  96.     if (di.physical[hpf].fWriteable        // If file is writeable, destroy cache
  97.     && di.physical[hpf].pdatadir)
  98.         {
  99.         MemFree(di.physical[hpf].pdatadir);
  100.         di.physical[hpf].pdatadir = NULL;
  101.         }
  102.     return TRUE;
  103.  
  104. ERROR_EXIT:
  105.     if (di.physical[hpf].pdatadir)
  106.         {
  107.         MemFree(di.physical[hpf].pdatadir);
  108.         di.physical[hpf].pdatadir = NULL;
  109.         }
  110.     di.physical[hpf].fl |= PF_ERROR;
  111.     return FALSE;
  112. }
  113.  
  114.  
  115. //----------------------------------------------------------------------------
  116. //   Description:    Release directory cache
  117. //    Parameters:    hpf        Physical file handle
  118. //       Returns:    TRUE if successful.
  119. //----------------------------------------------------------------------------
  120. BOOL FN_E DioDirRelease(HPF hpf)
  121. {
  122.     Assert(hpf >= 0 && hpf < MAX_PHYSICAL_FILES);    
  123.     Assert(di.physical[hpf].fUsed);
  124.     if (di.physical[hpf].pdatadir)
  125.         {
  126.         MemFree(di.physical[hpf].pdatadir);
  127.         di.physical[hpf].pdatadir = NULL;
  128.         }
  129.     return TRUE;
  130. }
  131.  
  132.  
  133. //----------------------------------------------------------------------------
  134. //   Description:    Release directory cache
  135. //    Parameters:    hpf        Physical file handle
  136. //       Returns:    TRUE if successful.
  137. //----------------------------------------------------------------------------
  138. BOOL FN_E DioDirReleaseAll(void)
  139. {
  140.     BOOL fResult = TRUE;
  141.     HPF hpf;
  142.  
  143.     for (hpf = 0; hpf < MAX_PHYSICAL_FILES; ++hpf)
  144.         if (di.physical[hpf].fUsed && !DioDirRelease(hpf))
  145.             fResult = FALSE;
  146.  
  147.     return fResult;
  148. }
  149. //----------------------------------------------------------------------------
  150. //------------------------------- End of File --------------------------------
  151. //----------------------------------------------------------------------------
  152.